/*Copyright (c) 2012 Christian Kunde
*
*Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
*The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
*
*THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*/

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package launcher;

import java.awt.Desktop;
import java.io.*;
import java.net.URI;
import java.net.URISyntaxException;
import javax.swing.DefaultListModel;

/**
 *
 * @author Chris
 * @version 1.0
 */
public class WoWLauncher extends javax.swing.JFrame {

    /**
     * Creates new form WoWLauncher
     */
    public WoWLauncher() {
        Save.setReadable(true,false);
        Save.setWritable(true, false);
        initComponents();
        Laden();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        RLList = new javax.swing.JDialog();
        jScrollPane1 = new javax.swing.JScrollPane();
        Favoriten = new javax.swing.JList();
        Eingabe = new javax.swing.JTextField();
        Change = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        Options = new javax.swing.JDialog();
        JTabbedPane1 = new javax.swing.JTabbedPane();
        Pfad = new javax.swing.JFileChooser();
        jPanel1 = new javax.swing.JPanel();
        EingabeNeu = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        Hinzufügen = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        Aktuell = new javax.swing.JList();
        jLabel3 = new javax.swing.JLabel();
        Entfernen = new javax.swing.JButton();
        Cache = new javax.swing.JButton();
        Realmlist = new javax.swing.JButton();
        Start = new javax.swing.JButton();
        jMenuBar1 = new javax.swing.JMenuBar();
        Optionen = new javax.swing.JMenu();
        About = new javax.swing.JMenu();

        RLList.setTitle("Realmlist Ändern");
        RLList.setAlwaysOnTop(true);
        RLList.setMinimumSize(new java.awt.Dimension(250, 250));

        Favoriten.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "1", "2", "3", "4", "5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        Favoriten.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        Favoriten.setVisibleRowCount(5);
        jScrollPane1.setViewportView(Favoriten);
        Favoriten.getAccessibleContext().setAccessibleName("");

        Change.setText("Ändern");
        Change.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ChangeActionPerformed(evt);
            }
        });

        jLabel1.setText("set realmist");

        javax.swing.GroupLayout RLListLayout = new javax.swing.GroupLayout(RLList.getContentPane());
        RLList.getContentPane().setLayout(RLListLayout);
        RLListLayout.setHorizontalGroup(
            RLListLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(RLListLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(RLListLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 205, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 205, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(Eingabe, javax.swing.GroupLayout.PREFERRED_SIZE, 205, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(Change, javax.swing.GroupLayout.PREFERRED_SIZE, 205, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        RLListLayout.setVerticalGroup(
            RLListLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(RLListLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 85, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(3, 3, 3)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(Eingabe, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(Change)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        Options.setTitle("Einstellungen");
        Options.setAlwaysOnTop(true);
        Options.setMinimumSize(new java.awt.Dimension(550, 350));

        Pfad.setApproveButtonText("Wählen");
        Pfad.setCurrentDirectory(new java.io.File("C:\\"));
            Pfad.setDialogTitle("WoW Pfad wählen");
            Pfad.setFileSelectionMode(javax.swing.JFileChooser.DIRECTORIES_ONLY);
            Pfad.setToolTipText("");
            Pfad.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    PfadActionPerformed(evt);
                }
            });
            JTabbedPane1.addTab("WoW Pfad", Pfad);

            jLabel2.setText("Neuer Favorit");

            Hinzufügen.setText("Hinzufügen");
            Hinzufügen.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    HinzufügenActionPerformed(evt);
                }
            });

            Aktuell.setModel(new javax.swing.AbstractListModel() {
                String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
                public int getSize() { return strings.length; }
                public Object getElementAt(int i) { return strings[i]; }
            });
            Aktuell.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
            jScrollPane2.setViewportView(Aktuell);

            jLabel3.setText("Aktuelle Favoriten");

            Entfernen.setText("Entfernen");
            Entfernen.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    EntfernenActionPerformed(evt);
                }
            });

            javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
            jPanel1.setLayout(jPanel1Layout);
            jPanel1Layout.setHorizontalGroup(
                jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel1Layout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(EingabeNeu)
                            .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, 195, Short.MAX_VALUE))
                        .addComponent(Hinzufügen, javax.swing.GroupLayout.PREFERRED_SIZE, 106, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGap(31, 31, 31)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 106, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 246, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(Entfernen, javax.swing.GroupLayout.PREFERRED_SIZE, 156, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addContainerGap(66, Short.MAX_VALUE))
            );
            jPanel1Layout.setVerticalGroup(
                jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel1Layout.createSequentialGroup()
                    .addGap(6, 6, 6)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel2)
                        .addComponent(jLabel3))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(jPanel1Layout.createSequentialGroup()
                            .addComponent(EingabeNeu, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                            .addComponent(Hinzufügen))
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(Entfernen)
                    .addContainerGap(121, Short.MAX_VALUE))
            );

            JTabbedPane1.addTab("Favoriten", jPanel1);

            javax.swing.GroupLayout OptionsLayout = new javax.swing.GroupLayout(Options.getContentPane());
            Options.getContentPane().setLayout(OptionsLayout);
            OptionsLayout.setHorizontalGroup(
                OptionsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(JTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 553, Short.MAX_VALUE)
            );
            OptionsLayout.setVerticalGroup(
                OptionsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(JTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 300, Short.MAX_VALUE)
            );

            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

            Cache.setText("Cache leeren");
            Cache.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    CacheActionPerformed(evt);
                }
            });

            Realmlist.setText("Realmlist ändern");
            Realmlist.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    RealmlistActionPerformed(evt);
                }
            });

            Start.setText("WoW Starten");
            Start.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    StartActionPerformed(evt);
                }
            });

            Optionen.setText("Einstellungen");
            Optionen.addMouseListener(new java.awt.event.MouseAdapter() {
                public void mouseClicked(java.awt.event.MouseEvent evt) {
                    OptionenMouseClicked(evt);
                }
            });
            Optionen.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    OptionenActionPerformed(evt);
                }
            });
            jMenuBar1.add(Optionen);

            About.setText("About");
            About.addMouseListener(new java.awt.event.MouseAdapter() {
                public void mouseClicked(java.awt.event.MouseEvent evt) {
                    AboutMouseClicked(evt);
                }
            });
            About.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    AboutActionPerformed(evt);
                }
            });
            jMenuBar1.add(About);

            setJMenuBar(jMenuBar1);

            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(Cache, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(Realmlist, javax.swing.GroupLayout.DEFAULT_SIZE, 156, Short.MAX_VALUE)
                .addComponent(Start, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(Cache)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                    .addComponent(Realmlist)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                    .addComponent(Start)
                    .addContainerGap())
            );

            pack();
        }// </editor-fold>//GEN-END:initComponents

    private void CacheActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CacheActionPerformed
        CacheLeeren();
    }//GEN-LAST:event_CacheActionPerformed

    private void RealmlistActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_RealmlistActionPerformed
        RLList.setVisible(true);
    }//GEN-LAST:event_RealmlistActionPerformed

    private void ChangeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ChangeActionPerformed
        RealmlistÄndern();
    }//GEN-LAST:event_ChangeActionPerformed

    private void StartActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_StartActionPerformed
        WoWStart();
    }//GEN-LAST:event_StartActionPerformed

    private void AboutActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_AboutActionPerformed
        
    }//GEN-LAST:event_AboutActionPerformed

    private void HinzufügenActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_HinzufügenActionPerformed
        try{
            DefaultListModel Favs = (DefaultListModel)Favoriten.getModel();
            Favs.addElement(EingabeNeu.getText());
            Speichern();
            Laden();
        }catch(Exception e){
            System.err.println(e);
        }                    
    }//GEN-LAST:event_HinzufügenActionPerformed

    private void PfadActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_PfadActionPerformed
        WoWPfad = Pfad.getCurrentDirectory();
        Speichern();
    }//GEN-LAST:event_PfadActionPerformed

    private void OptionenActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_OptionenActionPerformed
        
    }//GEN-LAST:event_OptionenActionPerformed

    private void OptionenMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_OptionenMouseClicked
        Options.setVisible(true);
    }//GEN-LAST:event_OptionenMouseClicked

    private void AboutMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_AboutMouseClicked
        try{
            Desktop.getDesktop().browse(new URI("http://jsource.tk"));
        }catch(URISyntaxException | IOException e){
            System.err.println(e);
        }
    }//GEN-LAST:event_AboutMouseClicked

    private void EntfernenActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_EntfernenActionPerformed
        int kill = Aktuell.getSelectedIndex();
        DefaultListModel favo = (DefaultListModel)Aktuell.getModel();
        favo.remove(kill);
        Speichern();
        Laden();
    }//GEN-LAST:event_EntfernenActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(WoWLauncher.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new WoWLauncher().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenu About;
    private javax.swing.JList Aktuell;
    private javax.swing.JButton Cache;
    private javax.swing.JButton Change;
    private javax.swing.JTextField Eingabe;
    private javax.swing.JTextField EingabeNeu;
    private javax.swing.JButton Entfernen;
    private javax.swing.JList Favoriten;
    private javax.swing.JButton Hinzufügen;
    private javax.swing.JTabbedPane JTabbedPane1;
    private javax.swing.JMenu Optionen;
    private javax.swing.JDialog Options;
    private javax.swing.JFileChooser Pfad;
    private javax.swing.JDialog RLList;
    private javax.swing.JButton Realmlist;
    private javax.swing.JButton Start;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    // End of variables declaration//GEN-END:variables
    //Eigene Variablen
    private File WoWPfad = new File("C:/Program Files/World of Warcraft");
    private File Save = new File(System.getProperty("user.dir")+"/config.txt");
    
    
    //Eigene Methoden
    
    private void CacheLeeren(){
        File WDB = new File(WoWPfad.toString()+"/Cache/WDB/deDE/");
        System.out.println(WDB.toString());
        for(File file : WDB.listFiles()){
            if(file.toString().endsWith(".wdb")){
                file.delete();
                }
            }
        }
    
    
    private void RealmlistÄndern(){
        File Realmliste = new File(WoWPfad.toString()+"/Data/deDE/realmlist.wtf");
        try{
            FileOutputStream Set = new FileOutputStream(Realmliste);
            if(!Favoriten.isSelectionEmpty()){
                Set.write(("set realmlist "+Favoriten.getSelectedValue().toString()).getBytes());
            }else{
                Set.write(("set realmlist "+Eingabe.getText()).getBytes());
            }
        }catch(Exception e){
            System.err.println(e);
        }
    }
    
    private void WoWStart(){
        File WoW = new File(WoWPfad.toString()+"/wow.exe");
        try{
            Runtime.getRuntime().exec(WoW.toString());
        }catch(Exception e){
            System.err.println(e);
        }
    }
    
    private void Laden(){
        try{
            try (BufferedReader Set = new BufferedReader(new java.io.FileReader(Save))) {
                if(Set.readLine().equalsIgnoreCase("--WoWPfad--")){
                    WoWPfad = new File(Set.readLine());
                }
                DefaultListModel favs = new DefaultListModel();
                String e = Set.readLine();
                while(e != null){
                    favs.addElement(e);
                    e = Set.readLine();
                }
                Favoriten.setModel(favs);
                Aktuell.setModel(favs);
            }
        }catch(Exception d){
            System.err.println(d);
        }
    }
    
    private void Speichern(){
        try{
            try (FileOutputStream Savr = new FileOutputStream(Save)) {
                Savr.write("--WoWPfad--\n".getBytes());
                Savr.write((WoWPfad.toString()+"\n").getBytes());
                int g = Favoriten.getModel().getSize();
                for(int i = 0; i < g;i++){
                    Savr.write((Favoriten.getModel().getElementAt(i).toString()+"\n").getBytes());
                }
            }
        }catch(Exception e){
            System.err.println(e);
        }        
    }
    


}
